import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:provider/provider.dart';
//import '../provied/tit.dart';
//import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
//import 'package:latlong/latlong.dart';
import 'package:city_pickers/city_pickers.dart';
//import '../map/address.dart';
import 'package:fluwx/fluwx.dart';
//import '../drawer/drawer.dart';
import '../config/api.dart';
//import 'dart:convert';
import '../provied/cartmodel.dart';
import '../model/cartmodellist.dart';
//import '../config/usersetting.dart';
//import 'package:amap_core_fluttify/amap_core_fluttify.dart';
//import 'package:flutter_map/flutter_map.dart'; e4d21525f7e7df4e6ac28c0177be6fa2
// await AmapService.init(iosKey: '7a***********************f4', androidKey: '7c***********************fa');
// /// 如果你觉得引擎的日志太多, 可以关闭Fluttify引擎的日志
// await enableFluttifyLog(false); // 关闭log

class MemberPage extends StatefulWidget {
  MemberPage({Key key}) : super(key: key);

  @override
  _MemberPageState createState() => _MemberPageState();
}

class _MemberPageState extends State<MemberPage> {
  get fluwx => null;
  String username;
 
  @override
  void initState() {
    _initFluwx();
    //_yantoken();
    _getads();
   setState(() {
      getusertoken();
       Addressl().getAddress();
        context.read<CartProvide>().getUsers();
        context.read<CartProvide>().getadminuser();
        context.read<CartProvide>().getOrder();
   });
  }
  void _getads(){
     context.read<CartProvide>().getUsers();
        context.read<CartProvide>().getadminuser();
        context.read<CartProvide>().getOrder();
  }
  //打开微信页面
  void loginWX() {
    // print("微信登录");
    fluwx
        .sendAuth(scope: "snsapi_userinfo", state: "wechat_sdk_demo_test")
        .then((data) {
      setState(() {
        print("拉取微信用户信息：" + data.toString());
      });
    }).catchError((e) {
      print('weChatLogin  e  $e');
    });
  }

  //跳转微信
  _initFluwx() async {
    print('登录');
    await registerWxApi(
        appId: "wxd930ea5d5a258f4f",
        doOnAndroid: true,
        doOnIOS: true,
        universalLink: "https://your.univerallink.com/link/");
    var result = await isWeChatInstalled;
    print("is installed $result");
  }

  //验证token
  // _yantoken() async {
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   setState(() {
  //     print(addresList[0]);
  //     print(prefs.getString('username'));
  //     username = prefs.getString('username');
  //     // print(json.encode(prefs.getString('username')).substring(1,4));
  //     // var c=json.encode(prefs.getString('username'));
  //     // this.username=c.substring(1,c.length-1);
  //     // print(c.substring(1,c.length-1));
  //   });
  //   // gettoken().then((value) => {
  //   //   print(value.data['message']['username']),
  //   //   if(value.data['err']==200){
  //   //     setState((){
  //   //       username=value.data['message']['username'];
  //   //     })
  //   //   }
  //   // });
  // }

  //final Distance distance = const Distance();
  Result resultArr = new Result();
  void _clickCity() async {
    Result result2 = await CityPickers.showFullPageCityPicker(
      context: context,
    );
    print(result2);
  }

  void _clickEventFunc() async {
    Result tempResult = await CityPickers.showCityPicker(
        context: context,
        theme:
            Theme.of(context).copyWith(primaryColor: Color(0xfffe1314)), // 设置主题
        locationCode: resultArr != null
            ? resultArr.areaId ?? resultArr.cityId ?? resultArr.provinceId
            : null, // 初始化地址信息
        cancelWidget: Text(
          '取消',
          style: TextStyle(
              fontSize: ScreenUtil().setSp(26), color: Color(0xff999999)),
        ),
        confirmWidget: Text(
          '确定',
          style: TextStyle(
              fontSize: ScreenUtil().setSp(26), color: Color(0xfffe1314)),
        ),
        height: 220.0);
    if (tempResult != null) {
      setState(() {
        resultArr = tempResult;
      });
    }
  }

  ScrollController _scrollController = new ScrollController();
  List<Map> c = [
    {
      "id": 0,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 1,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 2,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 3,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 4,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 5,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 7,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 8,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 9,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 10,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 11,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 12,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 13,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 14,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 15,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 16,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 17,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
    {
      "id": 18,
      "img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'
    },
  ];
  //跳转微信
  void _luancherurl() async {
    // String url = 'http://dbyxs.xyz';
    //  String url='tel:'+'1008611';
    String url = 'weixin://';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('访问失败');
    }
  }

  //拨打电话号码s
  void _luancherurls() async {
    // String url = 'http://dbyxs.xyz';
    String url = 'tel:' + '17091683061';
    //String url='weixin://';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('访问失败');
    }
  }

  //跳转高德地图
  void _launchURL() async {
    const url =
        'androidamap://keywordNavi?sourceApplication=softname&keyword=宛委山景区&style=2';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build(BuildContext context) {
    //  context.read<CartProvide>().getUsers();
    //     context.read<CartProvide>().getadminuser();
    //     context.read<CartProvide>().getOrder();
    return Scaffold(
       
        //  appBar: AppBar(),
        body: Container(
          child: Column(
            children: <Widget>[
              Container(
                width: double.infinity,
                height: ScreenUtil().setHeight(400),
                color: Colors.blue[200],
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      height: 35,
                    ),
                    Center(
                      child: Text('个人中心',
                          style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.w600,
                              fontSize: 18)),
                    ),
                    SizedBox(
                      height: 20,
                    ),
                    Container(
                      //  margin: EdgeInsets.only(top: 80),
                      // width: ScreenUtil().setWidth(750),
                      //  padding: EdgeInsets.all(20),
                      //  height: ScreenUtil().setHeight(100),
                      color: Colors.blue[200],
                      child: Container(
                        padding: EdgeInsets.all(20),
                        width: ScreenUtil().setWidth(650),
                        color: Colors.white,
                        height: ScreenUtil().setHeight(250),
                        // decoration: BoxDecoration(
                        // borderRadius: BorderRadius.all(Radius.circular(4.0)),
                        // borderRadius: BorderRadius.circular(15)
                        // ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                           InkWell(
                             onTap: (){
                                Navigator.pushNamed(context, '/image');
                               //final v=context.watch<CartProvide>().user;
                               //print(v);
                             },
                             child:  Container(
                              width: ScreenUtil().setWidth(140),
                              height: ScreenUtil().setWidth(140),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(70),
                                  image: DecorationImage(
                                      image: NetworkImage(
                                        context.watch<CartProvide>().userimages==null?'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12':'${context.watch<CartProvide>().userimages}'
                                      ),
                                      fit: BoxFit.cover)),
                            ),
                           ),
                            Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              //  crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                //  SizedBox( height: 30, ),
                                // SizedBox(width: 15,),
                                context.watch<CartProvide>().flname==false?Container(
                                  padding: EdgeInsets.only(left:10),
                                  width: ScreenUtil().setWidth(200),
                                  child: InkWell(
                                    onTap: (){
                                       Navigator.pushNamed(context, '/login');
                                    },
                                    child: Text('登录',style: TextStyle(color:Colors.black45),),
                                  ),
                                ):Container(
                                  padding: EdgeInsets.only(left:10),
                                  width: ScreenUtil().setWidth(300),
                                  child: Text('${ context.watch<CartProvide>().user}'),
                                ),
                                // Container(
                                //   width: ScreenUtil().setWidth(200),
                                //   child: context.watch<CartProvide>().user == null
                                //       ? InkWell(
                                //         onTap: (){
                                //           Navigator.pushNamed(context, '/login');
                                //         },
                                //         child: Text('登录'),
                                //       )
                                //       : Text(
                                //           '${context.watch<CartProvide>().user}',
                                //           style:
                                //               TextStyle(color: Colors.black45),
                                //         ),
                                // ),
                                Container(
                                   padding: EdgeInsets.only(left:10),
                                  width: ScreenUtil().setWidth(300),
                                  child: Text(
                                    '上次登录',
                                    style: TextStyle(
                                        color: Colors.black12, fontSize: 12),
                                  ),
                                ),
                                Container(
                                  padding: EdgeInsets.only(left:10),
                                  width: ScreenUtil().setWidth(300),
                                  child: addresList == null
                                      ? Text('收货地址')
                                      : Text('收货地址:${addresList[0]['addsl']}',style: TextStyle(fontSize:12),),
                                )
                                //Text('Running on: \n'),
                              ],
                            ),
                            // RaisedButton(onPressed: (){},child: Text('签到',style: TextStyle(fontSize:10),),color: Colors.redAccent,)
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
              //订单区域
              Container(
                child: Card(
                  child: Column(
                    children: <Widget>[
                      //订单头部区域
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            '    我的订单',
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w600),
                          ),
                          IconButton(
                              icon: Icon(Icons.chevron_right),
                              onPressed: () {
                                Navigator.pushNamed(context, '/order',
                                    arguments: {"id": 0});
                              })
                        ],
                      ),
                      //订单主体区域
                      Container(
                        height: ScreenUtil().setHeight(150),
                        child: Column(
                          // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Stack(
                                  children: <Widget>[
                                    Positioned(
                                      child: IconButton(
                                          icon: Icon(
                                            Icons.redeem,
                                            color: Colors.orange[100],
                                          ),
                                          onPressed: () {
                                            Navigator.pushNamed(
                                                context, '/order',
                                                arguments: {"id": 1});
                                          }),
                                    ),
                                    Positioned(
                                        top: 3,
                                        right: 5,
                                        child: Container(
                                          width: 15,
                                          height: 15,
                                          decoration: BoxDecoration(
                                            color: Colors.orangeAccent,
                                            borderRadius:
                                                BorderRadius.circular(10),
                                          ),
                                          child: Text(
                                            '${context.watch<CartProvide>().fu1}',
                                            textAlign: TextAlign.center,
                                            style:
                                                TextStyle(color: Colors.white),
                                          ),
                                        ))
                                  ],
                                ),
                                Stack(
                                  children: <Widget>[
                                    Positioned(
                                      child: IconButton(
                                          icon: Icon(
                                            Icons.store,
                                            color: Colors.orange[100],
                                          ),
                                          onPressed: () {
                                            Navigator.pushNamed(
                                                context, '/order',
                                                arguments: {"id": 2});
                                          }),
                                    ),
                                    Positioned(
                                        top: 3,
                                        right: 5,
                                        child: Container(
                                          width: 15,
                                          height: 15,
                                          decoration: BoxDecoration(
                                            color: Colors.orangeAccent,
                                            borderRadius:
                                                BorderRadius.circular(10),
                                          ),
                                          child: Text(
                                            '${context.watch<CartProvide>().fh }',
                                            textAlign: TextAlign.center,
                                            style:
                                                TextStyle(color: Colors.white),
                                          ),
                                        ))
                                  ],
                                ),
                                Stack(
                                  children: <Widget>[
                                    Positioned(
                                      child: IconButton(
                                          icon: Icon(
                                            Icons.send,
                                            color: Colors.orange[100],
                                          ),
                                          onPressed: () {
                                            Navigator.pushNamed(
                                                context, '/order',
                                                arguments: {"id": 3});
                                          }),
                                    ),
                                    Positioned(
                                        top: 3,
                                        right: 5,
                                        child: Container(
                                          width: 15,
                                          height: 15,
                                          decoration: BoxDecoration(
                                            color: Colors.orangeAccent,
                                            borderRadius:
                                                BorderRadius.circular(10),
                                          ),
                                          child: Text(
                                            '${context.watch<CartProvide>().sh }',
                                            textAlign: TextAlign.center,
                                            style:
                                                TextStyle(color: Colors.white),
                                          ),
                                        ))
                                  ],
                                ),
                                Stack(
                                  children: <Widget>[
                                    Positioned(
                                      child: IconButton(
                                          icon: Icon(
                                            Icons.swap_vertical_circle,
                                            color: Colors.orange[100],
                                          ),
                                          onPressed: () {
                                            Navigator.pushNamed(
                                                context, '/order',
                                                arguments: {"id": 4});
                                           
                                           // Navigator.pushNamed(context, '/video');
                                          }),
                                    ),
                                    Positioned(
                                        top: 3,
                                        right: 5,
                                        child: Container(
                                          width: 15,
                                          height: 15,
                                          decoration: BoxDecoration(
                                            color: Colors.orangeAccent,
                                            borderRadius:
                                                BorderRadius.circular(10),
                                          ),
                                          child: Text(
                                           // '${Provider.of<CartProvide>(context).count }',
                                           '${context.watch<CartProvide>().count }',
                                            textAlign: TextAlign.center,
                                            style:
                                                TextStyle(color: Colors.white),
                                          ),
                                        ))
                                  ],
                                )
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: <Widget>[
                                Text(
                                  '待付款',
                                  style: TextStyle(color: Colors.black38),
                                ),
                                Text('代发货',
                                    style: TextStyle(color: Colors.black38)),
                                Text('待收货',
                                    style: TextStyle(color: Colors.black38)),
                                Text('待退换',
                                    style: TextStyle(color: Colors.black38)),
                              ],
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ),
              //服务列表区域
              Container(
                child: Card(
                  child: Column(
                    children: <Widget>[
                      Container(
                        width: ScreenUtil().setWidth(700),
                        child: InkWell(
                          onTap: () {
                            Navigator.pushNamed(context, '/address');
                            // _launchURL();
                            // _clickEventFunc();
                            // Navigator.pushNamed(context, '/map',arguments: {"id":1});
                          },
                          child: Row(
                            // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(80),
                                child: Icon(
                                  Icons.location_on,
                                  color: Colors.orange[200],
                                ),
                              ),
                              Container(
                                width: ScreenUtil().setWidth(130),
                                child: Text('地址管理'),
                              ),
                              Container(
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(410)),
                                width: ScreenUtil().setWidth(490),
                                child: IconButton(
                                    icon: Icon(Icons.chevron_right),
                                    onPressed: null),
                              ),
                            ],
                          ),
                        ),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                                    width: 1, color: Colors.black12))),
                      ),
                      Container(
                        width: ScreenUtil().setWidth(700),
                        child: InkWell(
                          onTap: () {
                             Navigator.pushNamed(context, '/order',
                                    arguments: {"id": 0});
                          },
                          child: Row(
                            // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(80),
                                child: Icon(
                                  Icons.reorder,
                                  color: Colors.orange[200],
                                ),
                              ),
                              Container(
                                width: ScreenUtil().setWidth(130),
                                child: Text('全部订单'),
                              ),
                              Container(
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(410)),
                                width: ScreenUtil().setWidth(490),
                                child: IconButton(
                                    icon: Icon(Icons.chevron_right),
                                    onPressed: null),
                              ),
                            ],
                          ),
                        ),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                                    width: 1, color: Colors.black12))),
                      ),
                      Container(
                        width: ScreenUtil().setWidth(700),
                        child: InkWell(
                          onTap: () {
                            //  _luancherurl();
                            //  _initFluwx();
                            Navigator.pushNamed(context, '/map');
                          },
                          child: Row(
                            // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(80),
                                child: Icon(
                                  Icons.perm_phone_msg,
                                  color: Colors.orange[200],
                                ),
                              ),
                              Container(
                                width: ScreenUtil().setWidth(130),
                                child: Text('售后服务'),
                              ),
                              Container(
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(410)),
                                width: ScreenUtil().setWidth(490),
                                child: IconButton(
                                    icon: Icon(Icons.chevron_right),
                                    onPressed: () {}),
                              ),
                            ],
                          ),
                        ),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                                    width: 1, color: Colors.black12))),
                      ),
                      Container(
                        width: ScreenUtil().setWidth(700),
                        child: InkWell(
                          onTap: () {
                            // _clickCity();
                             _luancherurls();
                            // gettoken();
                           // _yantoken();
                            //  loginWX();
                            // _initFluwx();
                          },
                          child: Row(
                            // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(80),
                                child: Icon(
                                  Icons.touch_app,
                                  color: Colors.orange[200],
                                ),
                              ),
                              Container(
                                width: ScreenUtil().setWidth(130),
                                child: Text('联系客服'),
                              ),
                              Container(
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(410)),
                                width: ScreenUtil().setWidth(490),
                                child: IconButton(
                                    icon: Icon(Icons.chevron_right),
                                    onPressed: null),
                              ),
                            ],
                          ),
                        ),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                                    width: 1, color: Colors.black12))),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  // void getAddress() {}
}
